/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Tamina Framework.
  
  The Initial Developer of the Original Code is
  MOUTON David (aka damo)  <damo@tamina-online.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package org.tamina.utils.preload
{
	import mx.preloaders.*; 
    import flash.events.ProgressEvent;
    import org.tamina.core.init.InitManager;
    import org.tamina.core.init.InitEvent;
    import flash.events.Event;
   	import flash.utils.getDefinitionByName;
    import mx.events.FlexEvent;
    import org.tamina.core.lang.LangManager;
    import org.tamina.core.init.IInitializable;
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.display.LoaderInfo;

    public class SuperPreloaderClass extends DownloadProgressBar
    {
        //props
        private var __FlexInitialized:Boolean=false;
		private var __InitManagerInitialized:Boolean=false;
		private var __decoration:SuperPreloader;
		public  var __urlParameters:String = "applicationParameters.xml";
		private var __paramsXML:XML;

        
        //Constructeur
        public function SuperPreloaderClass()
        {   
            super();
           init();
 

            
        }
        
        public function init():void{
        	 //Add
            InitManager.addEventListener(InitEvent.INIT_COMPLETE,InitManagerComplete);
            InitManager.addEventListener(InitEvent.INIT_ELEMENT_FAILED,InitManagerComplete);
			InitManager.addEventListener(InitEvent.INIT_ELEMENT_COMPLETE,drawEvent);
			//Draw
			__decoration = new SuperPreloader();
			__decoration.x = this.stageWidth/2 - __decoration.width/2
			__decoration.y = this.stageHeight/2 - __decoration.height/2
			addChild(__decoration);
			//Params
			__paramsXML = new XML();
			var myLoader:URLLoader = new URLLoader(new URLRequest(__urlParameters));
			myLoader.addEventListener(Event.COMPLETE, paramsLoaded);
        }
        

        override  public function set preloader(preloader:Sprite):void {
            preloader.addEventListener(ProgressEvent.PROGRESS, myHandleProgress);   
            preloader.addEventListener(Event.COMPLETE, myHandleComplete);
            preloader.addEventListener(FlexEvent.INIT_PROGRESS, myHandleInitProgress);
            preloader.addEventListener(FlexEvent.INIT_COMPLETE, myHandleInitEnd);
        }

        //Methodes privées
    	private function paramsLoaded(e:Event):void{
			var l:URLLoader = e.target as URLLoader;
			l.removeEventListener(Event.COMPLETE,paramsLoaded);
    		__paramsXML = XML(l.data);
    		trace(__paramsXML.preloader.@applicationName+__FlexInitialized);
    		__decoration.setTitle(__paramsXML.preloader.@applicationName);
    		__decoration.setLogo(__paramsXML.preloader.@urlLogo);
    		__decoration.setStyle(__paramsXML.preloader.@urlCSS);
    		if(__FlexInitialized){
    			dispatchComplete();
    		}
    	}
    	

        private function myHandleProgress(event:ProgressEvent):void {
           __decoration.setInfo("Chargement de l'interface");
           __decoration.progressBar.setProgress(event.bytesLoaded/event.bytesTotal);
        }
    

        private function myHandleComplete(event:Event):void {
        	__decoration.progressBar.setProgress(100);
            __decoration.setInfo(" interface chargée");
        }
    

        private function myHandleInitProgress(event:Event):void {
        	__decoration.progressBar.setProgress(50);
            __decoration.setInfo(" initialisation de flex");
        }
    

        private function myHandleInitEnd(event:Event):void {
            __decoration.progressBar.setProgress(100);
            __decoration.setInfo("Flex initialized");
            __FlexInitialized = true;
            trace("flex initialized");
            dispatchComplete();
        }
        

        private function dispatchComplete():void {
        	__decoration.progressBar.setProgress(0);
			if(__paramsXML.length() > 0 && __paramsXML.children().length() > 0){
				trace("params ok : " + __paramsXML.toString());
				try {
	               var ClassReference:Class = getDefinitionByName(__paramsXML.preloader.init.@type) as Class;
	            }
	            catch(e:ReferenceError) {
	                trace(e);
	            }
           		var pc:IPreloadConfig = new ClassReference();
        		pc.setProperties(__paramsXML);
        		for(var i:uint;i<pc.getInitialisables().length;i++){
        			InitManager.addElement(pc.getInitialisables()[i] as IInitializable);
        		}
        		InitManager.start();
			}
        }
        
        private function InitManagerComplete(e:InitEvent):void{
        	__decoration.progressBar.setProgress(100);
        	InitManager.removeEventListener(InitEvent.INIT_COMPLETE,InitManagerComplete);
        	InitManager.removeEventListener(InitEvent.INIT_ELEMENT_COMPLETE,drawEvent);
        	__InitManagerInitialized = true;
        	if(__FlexInitialized){
        		preloadComplete();
        	}
        }
        private function preloadComplete():void{
        	trace("end");
        	dispatchEvent( new Event( Event.COMPLETE ) );
        }
        private function drawEvent(e:InitEvent):void{
        	__decoration.progressBar.setProgress(InitManager.getProgressPercent());
        	__decoration.setInfo(IInitializable(e.getData()).getName() + " initialized");
        }
       
    }

}